//
//  UpVersionViewController.swift
//  onemeetingmac
//
//  Created by 张衡 on 2020/10/10.
//  Copyright © 2020 onemeeting. All rights reserved.
//

import Foundation

class UpVersionViewController: NSViewController {
    override func loadView() {
        let view = NSView.init(frame: NSRect.init(x: 0, y: 0, width: 560, height: 400))
        view.wantsLayer = true
        view.layer?.backgroundColor = color_gray(233).cgColor
        self.view = view
    }
    
    override func viewWillAppear() {
        super.viewWillAppear()
        self.refactoringWindow()
    }
    
    func refactoringWindow() {
        self.view.window?.title = "软件更新"
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.setupUI()
        self.updateButtom.becomeFirstResponder()
    }
    
    func setupUI() {
        self.view.addSubview(icon)
        icon.snp.makeConstraints { (make) in
            make.width.height.equalTo(60)
            make.top.equalTo(30)
            make.left.equalTo(30)
        }
        
        self.view.addSubview(rightView)
        rightView.snp.makeConstraints { (make) in
            make.left.equalTo(icon.snp.right).offset(10)
            make.right.equalTo(-20)
            make.top.equalTo(25)
            make.bottom.equalTo(-20)
        }
        
        self.rightView.addSubview(stitle)
        stitle.snp.makeConstraints { (make) in
            make.height.equalTo(20)
            make.top.equalToSuperview()
            make.left.equalToSuperview()
        }
        
        self.rightView.addSubview(mssage)
        mssage.snp.makeConstraints { (make) in
            make.height.equalTo(20)
            make.top.equalTo(stitle.snp.bottom).offset(5)
            make.left.equalToSuperview()
        }
        
        self.rightView.addSubview(upTitle)
        upTitle.snp.makeConstraints { (make) in
            make.height.equalTo(20)
            make.top.equalTo(mssage.snp.bottom).offset(2)
            make.left.equalToSuperview()
        }
        
        
        self.rightView.addSubview(updateButtom)
        updateButtom.snp.makeConstraints { (make) in
            make.width.equalTo(100)
            make.bottom.equalTo(0)
            make.right.equalToSuperview()
        }
        
        self.rightView.addSubview(autoButtom)
        autoButtom.snp.makeConstraints { (make) in
            make.bottom.equalTo(updateButtom.snp.top).offset(-20)
            make.left.equalToSuperview()
        }
        
        self.rightView.addSubview(upTextView)
        upTextView.snp.makeConstraints { (make) in
            make.width.equalToSuperview()
            make.top.equalTo(upTitle.snp.bottom)
            make.bottom.equalTo(autoButtom.snp.top).offset(-10)
            make.left.equalToSuperview()
        }
        
        
    }
    
    lazy var icon: NSImageView = {
        let imageV = NSImageView.init(image: NSImage.image(name:"ic_logo_blue")!)
        return imageV
    }()
    
    lazy var rightView: NSView = {
        let view = NSView.init()
        return view
    }()
    
    lazy var stitle: NSLabel = {
        let lab = NSLabel.label(title: "新版本 壹会通 发布了", textColor: color_gray(0), font: NSFont.boldSystemFont(ofSize: 15))
        lab.sizeToFit()
        return lab
    }()
    
    lazy var mssage: NSLabel = {
        let lab = NSLabel.label(title: "新版本 2.5.0 当前版本 2.4.3，你确定要更新吗？", textColor: color_gray(0), font: NSFont.systemFont(ofSize:12))
        lab.sizeToFit()
        return lab
    }()
    
    lazy var upTitle: NSLabel = {
        let lab = NSLabel.label(title: "更新信息", textColor: color_gray(0), font: NSFont.boldSystemFont(ofSize: 12))
        lab.sizeToFit()
        return lab
    }()
    
    lazy var upTextView: NSTextView = {
        
        let layoutManger = NSLayoutManager.init()
        
        let mutaleString = NSMutableAttributedString.init()
        let attString1 = NSAttributedString.init(string: " 2.5.0.15", attributes: [.font : NSFont.boldSystemFont(ofSize: 20)])
        mutaleString.append(attString1)
        
        let paragraphStyle = NSMutableParagraphStyle.init()
        paragraphStyle.lineSpacing = 2// 行距
        paragraphStyle.lineBreakMode = .byTruncatingTail//结尾部分的内容以……方式省略 ( "...wxyz" ,"abcd..." ,"ab...yz")
        paragraphStyle.firstLineHeadIndent = 0;//整体缩进(首行除外)
        paragraphStyle.headIndent = 0;//整体缩进(首行除外)
        paragraphStyle.tailIndent = 300;//
        paragraphStyle.minimumLineHeight = 10;//最低行高
        paragraphStyle.maximumLineHeight = 16;//最大行高
        paragraphStyle.paragraphSpacing = 0;//段与段之间的间距
        paragraphStyle.paragraphSpacingBefore = 0;//段首行空白空间/* Distance between the bottom of the previous paragraph (or the end of its paragraphSpacing, if any) and the top of this paragraph. */
        paragraphStyle.baseWritingDirection = .leftToRight;//从左到右的书写方向（一共➡️三种）
        paragraphStyle.lineHeightMultiple = 1;/* Natural line height is multiplied by this factor (if positive) before being constrained by minimum and maximum line height. */
        paragraphStyle.hyphenationFactor = 1;//连字属性 在iOS，唯一支持的值分别为0和1
        let attString2 = NSAttributedString.init(string: "\n\n ◆ 修复一万个bug\n ◆ 提高软件稳定性修复\n ◆ 一万个bug。。提高软件稳定性", attributes: [
            .font : NSFont.systemFont(ofSize: 14),
            .paragraphStyle: paragraphStyle
        ])
        mutaleString.append(attString2)
        let textStorage1 = NSTextStorage.init(attributedString: mutaleString)
        textStorage1.addLayoutManager(layoutManger)
        
        
        let size = CGSize.init(width: 300, height: 540)
        let textContrainer1 = NSTextContainer.init(size: size)
        layoutManger.addTextContainer(textContrainer1)
        let textContrainer2 = NSTextContainer.init(size: size)
        layoutManger.addTextContainer(textContrainer2)
        let textContrainer3 = NSTextContainer.init(size: size)
        layoutManger.addTextContainer(textContrainer3)
        let textContrainer4 = NSTextContainer.init(size: size)
        layoutManger.addTextContainer(textContrainer4)
        
        let view = NSTextView.init(frame: NSRect.zero, textContainer: textContrainer1)
        //view.minSize = NSMakeSize(300, 100)
        //view.maxSize = NSMakeSize(CGFloat(Float.greatestFiniteMagnitude), CGFloat(Float.greatestFiniteMagnitude))
        view.isEditable = false
        view.isVerticallyResizable = true
        view.isHorizontallyResizable = false
        view.isSelectable = true
        
        view.textColor = color_gray(20)
        //view.layoutManager?.delegate = self
        view.textContainer?.containerSize = NSMakeSize(CGFloat(Float.greatestFiniteMagnitude), CGFloat(Float.greatestFiniteMagnitude))
        view.textContainer?.widthTracksTextView = true
        view.textContainer?.maximumNumberOfLines = 10
        view.invalidateIntrinsicContentSize()
        view.wantsLayer = true
        view.layer?.borderColor = color_gray(0).cgColor
        view.layer?.borderWidth = 1
        return view
    }()
    
    lazy var autoButtom: NSButton = {
        let btn = NSButton.init()
        btn.title = "以后自动更新和下载安装"
        btn.setButtonType(NSButton.ButtonType.switch)
        btn.state = .on
        btn.font = NSFont.systemFont(ofSize: 12)
        btn.sizeToFit()
        return btn
    }()
    
    lazy var updateButtom: NSButton = {
        let btn = NSButton.init()
        btn.title = "安装更新"
        btn.bezelStyle = .rounded
        btn.setButtonType(NSButton.ButtonType.pushOnPushOff)
        btn.state = .on
        btn.font = NSFont.systemFont(ofSize: 13)
        btn.keyEquivalent = "\r"
        btn.sizeToFit()
        return btn
    }()
}
